import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_test_demo/dialog/dialog_util.dart';
import 'package:flutter_test_demo/three/animate_page.dart';
import 'package:flutter_test_demo/three/badges_page.dart';
import 'package:flutter_test_demo/three/device_page.dart';
import 'package:flutter_test_demo/three/down_page.dart';
import 'package:flutter_test_demo/three/fresh_state.dart';
import 'package:flutter_test_demo/three/html_page.dart';
import 'package:flutter_test_demo/three/http_get_page.dart';
import 'package:flutter_test_demo/three/image_page.dart';
import 'package:flutter_test_demo/three/image_picker_page.dart';
import 'package:flutter_test_demo/three/life_page.dart';
import 'package:flutter_test_demo/three/luck_page.dart';
import 'package:flutter_test_demo/three/package_page.dart';
import 'package:flutter_test_demo/three/permission_state.dart';
import 'package:flutter_test_demo/three/share_page.dart';
import 'package:flutter_test_demo/three/webview_page.dart';
import 'package:flutter_test_demo/webplugin/web_plugin_page.dart';
import 'package:logger/logger.dart';

import 'alioss/aliyun2_page.dart';
import 'alioss/aliyun_page.dart';
import 'dialog/smart_dialog_page.dart';
import 'dialog/toast_context.dart';
import 'dialog/toast_no_context.dart';
import 'edit/edit_page.dart';
import 'edit/edit_visibility_page.dart';
import 'innerweb/common_web_page.dart';
import 'innerweb/demo_screen.dart';
import 'innerweb/html_web_page.dart';
import 'innerweb/inner_web_page.dart';
import 'innerweb/msg_channel_page.dart';
import 'lottery/lottery_page.dart';
import 'three/HttpPage.dart';
import 'three/anim/transitions_page.dart';
import 'three/my_web_page.dart';
import 'three/notification_page.dart';
import 'three/screen_page.dart';
typedef MyTapCallback = void Function();
GlobalKey globalKey = GlobalKey();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterDownloader.initialize(
      debug: true
  );
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(360, 690),
      splitScreenMode: true,
      builder: () => MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter_ScreenUtil',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          textTheme: TextTheme(button: TextStyle(fontSize: 14.sp)),
        ),
        builder: (context, widget) {
          return MediaQuery(
            //Setting font does not change with system font size
            data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
            child: widget ?? Padding(padding: EdgeInsets.only()),
          );
        },
        home: MyHomePage(title: 'FlutterScreenUtil Demo'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  late BuildContext context;
  final List<String> _list = [];
  final List<MyTapCallback> _taps = [];

  @override
  Widget build(BuildContext context) {
    this.context = context;
    _titles();
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: ListView.builder(
          itemBuilder: (context, index){
            return ListTile(
              title: Text(_list[index]),
              onTap: _taps[index],
            );
          },
          itemCount: _list.length,
        ),
      ),
    );
  }

  /// bottomNavigation
  /// https://gitee.com/doner/flutter_ducafecat_news_getx?_from=gitee_search

  _titles(){
    _list.clear();
    _taps.clear();

    _list.add('AliyunPage');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => AliyunPage()),);
    });

    _list.add('AliyunPage2');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => Aliyun2Page()),);
    });

    _list.add('DownPage');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => DownPage()),);
    });

    _list.add('CommonWebPage');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => CommonWebPage()),);
    });

    _list.add('MsgChannel');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => MsgChannelPage()),);
    });

    _list.add('Htmlweb');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => HtmlwebApp()),);
    });

    _list.add('DemoScreen');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => DemoScreen()),);
    });

    _list.add('Webview');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => WebviewPage()),);
    });

    _list.add('MyWeb');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => MyWebPage()),);
    });

    _list.add('WebPlugin');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => WebPluginPage()),);
    });

    _list.add('InnerWeb');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => InnerWebPage()),);
    });

    _list.add('通知');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => NotificationPage()),);
    });

    _list.add('软键盘');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => EditVisibilityPage()),);
    });

    _list.add('编辑');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => EditPage()),);
    });

    _list.add('自定义toast');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => ToastContext()),);
    });

    _list.add('自定义toast no');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => ToastNoContext()),);
    });

    _list.add('对话框');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => SmartDialogPage()),);
    });
    FlutterSmartDialog.init();

    _list.add('网络请求GET');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => HttpGetPage()),);
    });

    _list.add('生命周期');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => LifePage()),);
    });

    _list.add('分享');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => SharePage()),);
    });

    _list.add('缩放');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => AnimatePage()),);
    });

    _list.add('动画');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => TransitionsPage()),);
    });

    _list.add('刷新');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => FreshState()),);
    });

    _list.add('权限');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => PermissionHandlerWidgetState()),);
    });

    _list.add('Widget 转 Image');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => ImagePage()),);
    });
    _list.add('dio封装');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => HttpPage()),);
    });

    _list.add('dialog');
    _taps.add((){
      DialogUtil.perDialog(context);
    });

    _list.add('Image Picker');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => ImagePickerPage()),);
    });

    _list.add('打印print');
    _taps.add((){
      log();
    });

    _list.add('屏幕适配');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => ScreenPage()),);
    });

    _list.add('Badge');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => BadgesScreen()),);
    });

    _list.add('Package');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => PackagePage()),);
    });

    _list.add('设备信息');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => DevicePage()),);
    });

    _list.add('抽奖转盘');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => LotteryPage()),);
    });

    _list.add('GridView抽奖');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => LuckPage()),);
    });

    _list.add('HTML');
    _taps.add((){
      Navigator.push(context, MaterialPageRoute(builder: (context) => HtmlPage()),);
    });

  }

  void log() {
    var logger = Logger(
      printer: PrettyPrinter(),
    );

    var loggerNoStack = Logger(
      printer: PrettyPrinter(methodCount: 0),
    );

    logger.d('Log message with 2 methods');
    logger.i('Log message with 2 methods');
    logger.v('Log message with 2 methods');
    logger.w('Log message with 2 methods');
    logger.wtf('Log message with 2 methods');

    loggerNoStack.i('Info message');

    loggerNoStack.w('Just a warning!');

    logger.e('Error! Something bad happened', 'Test Error');

    loggerNoStack.v({'key': 5, 'value': 'something'});

    Logger(printer: SimplePrinter(colors: true)).v('boom');
  }
}
